package ma.android.oook;

import java.util.ArrayList;
import java.util.Currency;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener{
	/** Called when the activity is first created. */

	private static final int BIG_BUTTON_PADDING = 15;
	private static final String OOOK_PREF_FILE="OookFile";
	
	private ArrayList<Button> upButtons = new ArrayList<Button>();
	private OookDbAdapter mDbHelper;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDbHelper = new OookDbAdapter(this);
		mDbHelper.open();
		if (mDbHelper.findTags()== null)
			mDbHelper.createTags();
		
		SharedPreferences preferences = getSharedPreferences(OOOK_PREF_FILE, MODE_PRIVATE);
		if (preferences.getAll().size() == 0)
		{
			Currency currency = Currency.getInstance(Locale.getDefault());
			Locale l = Locale.getDefault();
			
			SharedPreferences.Editor editor = preferences.edit();
			editor.putString(getString(R.string.language), l.getISO3Language());
			editor.putString(getString(R.string.currency), currency.getCurrencyCode());
			editor.putString(getString(R.string.currencySymbol), currency.getSymbol());
			editor.putInt(getString(R.string.dateType), 1);
			editor.commit();
		}
		
		setContentView(R.layout.main);
		
		WindowManager w = getWindowManager();
		Display d = w.getDefaultDisplay();

		int width = d.getWidth();
		int upButtonSizeX = (width - BIG_BUTTON_PADDING * 4)/2;

		Button newLoan = (Button)findViewById(R.id.newLoan); 
		newLoan.setOnClickListener(this);
		upButtons.add(newLoan);
		Button newBorrow = (Button)findViewById(R.id.newBorrow); 
		newBorrow.setOnClickListener(this);
		upButtons.add(newBorrow);
		Button listLoans = (Button)findViewById(R.id.listLoans); 
		listLoans.setOnClickListener(this);
		upButtons.add(listLoans);
		Button listBorrowed = (Button)findViewById(R.id.listBorrowed);
		listBorrowed.setOnClickListener(this);
		upButtons.add(listBorrowed);
		
		for (Button button : upButtons) {
			button.setMinimumWidth(upButtonSizeX);
		}
		OookItem [] closeReminders = findCloserReminders();
		if (closeReminders != null)
			createAds(closeReminders);
		totalMoney();
		mDbHelper.close();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		launchActivity(v.getId());
	}
	public void launchActivity(int type)
	{
		switch(type)
		{
			case R.id.newLoan:		newLoan();
									break;
			
			case R.id.minewLoan:	newLoan();
									break;
	
			case R.id.newBorrow:	newBorrow();
									break;
									
			case R.id.minewBorrow:	newBorrow();
									break;
	
			case R.id.listLoans:	listLoans();
									break;
									
			case R.id.milistLoans:	listLoans();
									break;
	
			case R.id.listBorrowed:	listBorrowed();
									break;
									
			case R.id.milistBorrowed:listBorrowed();
									break;
									
			case R.id.miSettings: 	settings();
									break;
		}
	}

	public void totalMoney()
	{
		Cursor loans = mDbHelper.totalLoans();
		Cursor debts = mDbHelper.totalDebts();
		SharedPreferences preferences = getSharedPreferences(OOOK_PREF_FILE, MODE_PRIVATE);
		String currencySymbol = preferences.getString(getString(R.string.currencySymbol), "");
		String text = OookUtils.recoverMoneyTotals(loans, debts, currencySymbol,this);	
		TextView money = (TextView) findViewById(R.id.money);
		money.setText(text);
	}
	public OookItem [] findCloserReminders()
	{
		OookItem [] closerReminders = null;
		Cursor items = mDbHelper.findAll();
		if (items != null && items.getCount()>0)
		{
			startManagingCursor(items);
			closerReminders= new OookItem[3];
			while(!items.isLast())
			{
				items.moveToNext();
				OookItem item = OookUtils.createItemFromCursor(items);
				closerReminders = OookUtils.orderCloserReminders(closerReminders, item);
			}
		}
		items.close();
		return closerReminders;
	}
	public void createAds(OookItem reminders[])
	{
		SharedPreferences preferences = getSharedPreferences(OOOK_PREF_FILE, MODE_PRIVATE);
		String currencySymbol = preferences.getString(getString(R.string.currencySymbol), "");
		int dateType = preferences.getInt(getString(R.string.dateType), 0);
		String ads [] = new String [3];
		ads = OookUtils.createStringAds(reminders, currencySymbol,dateType,this);
		TextView reminder1 = (TextView) findViewById(R.id.reminder1);
		reminder1.setText(ads[0]);
		TextView reminder2 = (TextView) findViewById(R.id.reminder2);
		reminder2.setText(ads[1]);
		TextView reminder3 = (TextView) findViewById(R.id.reminder3);
		reminder3.setText(ads[2]);
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if (requestCode == resultCode || requestCode == 3)
		{	
			finish();
			startActivity(getIntent());
		}
	}
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.mainmenu, menu);
	    return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) 
	{
	   launchActivity(item.getItemId());
	   return false;
	}
	public void newLoan()
	{
		Intent intent = new Intent(this,NewItem.class);
		intent.putExtra(getString(R.string.modeType), getString(R.string.newMode));
		intent.putExtra(getString(R.string.operationType), getString(R.string.loan));
		startActivityForResult(intent, 1);
	}
	public void newBorrow()
	{
		Intent intent = new Intent(this, NewItem.class);
		intent.putExtra(getString(R.string.modeType), getString(R.string.newMode));
		intent.putExtra(getString(R.string.operationType), getString(R.string.borrow));
		startActivityForResult(intent, 1);
	}
	
	public void listLoans()
	{
		Intent intent = new Intent(this, ListItems.class);
		intent.putExtra(getString(R.string.operationType), getString(R.string.loans));
		startActivityForResult(intent,2);
	}
	public void listBorrowed()
	{
		Intent intent = new Intent(this, ListItems.class);
		intent.putExtra(getString(R.string.operationType), getString(R.string.borrowed));
		startActivityForResult(intent,2);
	}
	public void settings()
	{
		Intent intent = new Intent(this, OookPreferences.class);
		startActivityForResult(intent,3);
	}
}